3 use Wikimedia\TestingAccessWrapper
;
7 * ^--- trigger DB shadowing because we are using Title magic
9 class ParserOutputTest
extends MediaWikiTestCase
{
11 public static function provideIsLinkInternal() {
14 [ false, 'http://example.org', 'http://mediawiki.org' ],
16 [ true, 'http://example.org', 'http://example.org' ],
17 [ true, 'https://example.org', 'https://example.org' ],
18 [ true, '//example.org', '//example.org' ],
19 // Same domain different cases
20 [ true, 'http://example.org', 'http://EXAMPLE.ORG' ],
21 // Paths, queries, and fragments are not relevant
22 [ true, 'http://example.org', 'http://example.org/wiki/Main_Page' ],
23 [ true, 'http://example.org', 'http://example.org?my=query' ],
24 [ true, 'http://example.org', 'http://example.org#its-a-fragment' ],
25 // Different protocols
26 [ false, 'http://example.org', 'https://example.org' ],
27 [ false, 'https://example.org', 'http://example.org' ],
28 // Protocol relative servers always match http and https links
29 [ true, '//example.org', 'http://example.org' ],
30 [ true, '//example.org', 'https://example.org' ],
31 // But they don't match strange things like this
32 [ false, '//example.org', 'irc://example.org' ],
37 * Test to make sure ParserOutput::isLinkInternal behaves properly
38 * @dataProvider provideIsLinkInternal
39 * @covers ParserOutput::isLinkInternal
41 public function testIsLinkInternal( $shouldMatch, $server, $url ) {
42 $this->assertEquals( $shouldMatch, ParserOutput
::isLinkInternal( $server, $url ) );
46 * @covers ParserOutput::setExtensionData
47 * @covers ParserOutput::getExtensionData
49 public function testExtensionData() {
50 $po = new ParserOutput();
52 $po->setExtensionData( "one", "Foo" );
54 $this->assertEquals( "Foo", $po->getExtensionData( "one" ) );
55 $this->assertNull( $po->getExtensionData( "spam" ) );
57 $po->setExtensionData( "two", "Bar" );
58 $this->assertEquals( "Foo", $po->getExtensionData( "one" ) );
59 $this->assertEquals( "Bar", $po->getExtensionData( "two" ) );
61 $po->setExtensionData( "one", null );
62 $this->assertNull( $po->getExtensionData( "one" ) );
63 $this->assertEquals( "Bar", $po->getExtensionData( "two" ) );
67 * @covers ParserOutput::setProperty
68 * @covers ParserOutput::getProperty
69 * @covers ParserOutput::unsetProperty
70 * @covers ParserOutput::getProperties
72 public function testProperties() {
73 $po = new ParserOutput();
75 $po->setProperty( 'foo', 'val' );
77 $properties = $po->getProperties();
78 $this->assertEquals( $po->getProperty( 'foo' ), 'val' );
79 $this->assertEquals( $properties['foo'], 'val' );
81 $po->setProperty( 'foo', 'second val' );
83 $properties = $po->getProperties();
84 $this->assertEquals( $po->getProperty( 'foo' ), 'second val' );
85 $this->assertEquals( $properties['foo'], 'second val' );
87 $po->unsetProperty( 'foo' );
89 $properties = $po->getProperties();
90 $this->assertEquals( $po->getProperty( 'foo' ), false );
91 $this->assertArrayNotHasKey( 'foo', $properties );
95 * @covers ParserOutput::getText
96 * @dataProvider provideGetText
97 * @param array $options Options to getText()
98 * @param array $poState ParserOptions state fields to set
99 * @param string $text Parser text
100 * @param string $expect Expected output
102 public function testGetText( $options, $poState, $text, $expect ) {
103 $this->setMwGlobals( [
104 'wgArticlePath' => '/wiki/$1',
105 'wgScriptPath' => '/w',
106 'wgScript' => '/w/index.php',
109 $po = new ParserOutput( $text );
112 $po->setEditSectionTokens( true );
115 $wrap = TestingAccessWrapper
::newFromObject( $po );
116 foreach ( $poState as $key => $value ) {
117 $wrap->$key = $value;
121 $actual = $po->getText( $options );
122 $this->assertSame( $expect, $actual );
125 public static function provideGetText() {
126 // phpcs:disable Generic.Files.LineLength
130 <mw:toc><div id="toc" class="toc"><div class="toctitle"><h2>Contents</h2></div>
132 <li class="toclevel-1 tocsection-1"><a href="#Section_1"><span class="tocnumber">1</span> <span class="toctext">Section 1</span></a></li>
133 <li class="toclevel-1 tocsection-2"><a href="#Section_2"><span class="tocnumber">2</span> <span class="toctext">Section 2</span></a>
135 <li class="toclevel-2 tocsection-3"><a href="#Section_2.1"><span class="tocnumber">2.1</span> <span class="toctext">Section 2.1</span></a></li>
138 <li class="toclevel-1 tocsection-4"><a href="#Section_3"><span class="tocnumber">3</span> <span class="toctext">Section 3</span></a></li>
142 <h2><span class="mw-headline" id="Section_1">Section 1</span><mw:editsection page="Test Page" section="1">Section 1</mw:editsection></h2>
145 <h2><span class="mw-headline" id="Section_2">Section 2</span><mw:editsection page="Test Page" section="2">Section 2</mw:editsection></h2>
148 <h3><span class="mw-headline" id="Section_2.1">Section 2.1</span><mw:editsection page="Test Page" section="3">Section 2.1</mw:editsection></h3>
151 <h2><span class="mw-headline" id="Section_3">Section 3</span><mw:editsection page="Test Page" section="4">Section 3</mw:editsection></h2>
157 'No stateless options, default state' => [
158 [], [], $text, <<<EOF
161 <div id="toc" class="toc"><div class="toctitle"><h2>Contents</h2></div>
163 <li class="toclevel-1 tocsection-1"><a href="#Section_1"><span class="tocnumber">1</span> <span class="toctext">Section 1</span></a></li>
164 <li class="toclevel-1 tocsection-2"><a href="#Section_2"><span class="tocnumber">2</span> <span class="toctext">Section 2</span></a>
166 <li class="toclevel-2 tocsection-3"><a href="#Section_2.1"><span class="tocnumber">2.1</span> <span class="toctext">Section 2.1</span></a></li>
169 <li class="toclevel-1 tocsection-4"><a href="#Section_3"><span class="tocnumber">3</span> <span class="toctext">Section 3</span></a></li>
173 <h2><span class="mw-headline" id="Section_1">Section 1</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&action=edit&section=1" title="Edit section: Section 1">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
176 <h2><span class="mw-headline" id="Section_2">Section 2</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&action=edit&section=2" title="Edit section: Section 2">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
179 <h3><span class="mw-headline" id="Section_2.1">Section 2.1</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&action=edit&section=3" title="Edit section: Section 2.1">edit</a><span class="mw-editsection-bracket">]</span></span></h3>
182 <h2><span class="mw-headline" id="Section_3">Section 3</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&action=edit&section=4" title="Edit section: Section 3">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
187 'No stateless options, TOC statefully disabled' => [
188 [], [ 'mTOCEnabled' => false ], $text, <<<EOF
192 <h2><span class="mw-headline" id="Section_1">Section 1</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&action=edit&section=1" title="Edit section: Section 1">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
195 <h2><span class="mw-headline" id="Section_2">Section 2</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&action=edit&section=2" title="Edit section: Section 2">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
198 <h3><span class="mw-headline" id="Section_2.1">Section 2.1</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&action=edit&section=3" title="Edit section: Section 2.1">edit</a><span class="mw-editsection-bracket">]</span></span></h3>
201 <h2><span class="mw-headline" id="Section_3">Section 3</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&action=edit&section=4" title="Edit section: Section 3">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
206 'No stateless options, section edits statefully disabled' => [
207 [], [ 'mEditSectionTokens' => false ], $text, <<<EOF
210 <div id="toc" class="toc"><div class="toctitle"><h2>Contents</h2></div>
212 <li class="toclevel-1 tocsection-1"><a href="#Section_1"><span class="tocnumber">1</span> <span class="toctext">Section 1</span></a></li>
213 <li class="toclevel-1 tocsection-2"><a href="#Section_2"><span class="tocnumber">2</span> <span class="toctext">Section 2</span></a>
215 <li class="toclevel-2 tocsection-3"><a href="#Section_2.1"><span class="tocnumber">2.1</span> <span class="toctext">Section 2.1</span></a></li>
218 <li class="toclevel-1 tocsection-4"><a href="#Section_3"><span class="tocnumber">3</span> <span class="toctext">Section 3</span></a></li>
222 <h2><span class="mw-headline" id="Section_1">Section 1</span></h2>
225 <h2><span class="mw-headline" id="Section_2">Section 2</span></h2>
228 <h3><span class="mw-headline" id="Section_2.1">Section 2.1</span></h3>
231 <h2><span class="mw-headline" id="Section_3">Section 3</span></h2>
236 'Stateless options override stateful settings' => [
237 [ 'allowTOC' => true, 'enableSectionEditLinks' => true ],
238 [ 'mTOCEnabled' => false, 'mEditSectionTokens' => false ],
242 <div id="toc" class="toc"><div class="toctitle"><h2>Contents</h2></div>
244 <li class="toclevel-1 tocsection-1"><a href="#Section_1"><span class="tocnumber">1</span> <span class="toctext">Section 1</span></a></li>
245 <li class="toclevel-1 tocsection-2"><a href="#Section_2"><span class="tocnumber">2</span> <span class="toctext">Section 2</span></a>
247 <li class="toclevel-2 tocsection-3"><a href="#Section_2.1"><span class="tocnumber">2.1</span> <span class="toctext">Section 2.1</span></a></li>
250 <li class="toclevel-1 tocsection-4"><a href="#Section_3"><span class="tocnumber">3</span> <span class="toctext">Section 3</span></a></li>
254 <h2><span class="mw-headline" id="Section_1">Section 1</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&action=edit&section=1" title="Edit section: Section 1">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
257 <h2><span class="mw-headline" id="Section_2">Section 2</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&action=edit&section=2" title="Edit section: Section 2">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
260 <h3><span class="mw-headline" id="Section_2.1">Section 2.1</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&action=edit&section=3" title="Edit section: Section 2.1">edit</a><span class="mw-editsection-bracket">]</span></span></h3>
263 <h2><span class="mw-headline" id="Section_3">Section 3</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&action=edit&section=4" title="Edit section: Section 3">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
268 'Statelessly disable section edit links' => [
269 [ 'enableSectionEditLinks' => false ], [], $text, <<<EOF
272 <div id="toc" class="toc"><div class="toctitle"><h2>Contents</h2></div>
274 <li class="toclevel-1 tocsection-1"><a href="#Section_1"><span class="tocnumber">1</span> <span class="toctext">Section 1</span></a></li>
275 <li class="toclevel-1 tocsection-2"><a href="#Section_2"><span class="tocnumber">2</span> <span class="toctext">Section 2</span></a>
277 <li class="toclevel-2 tocsection-3"><a href="#Section_2.1"><span class="tocnumber">2.1</span> <span class="toctext">Section 2.1</span></a></li>
280 <li class="toclevel-1 tocsection-4"><a href="#Section_3"><span class="tocnumber">3</span> <span class="toctext">Section 3</span></a></li>
284 <h2><span class="mw-headline" id="Section_1">Section 1</span></h2>
287 <h2><span class="mw-headline" id="Section_2">Section 2</span></h2>
290 <h3><span class="mw-headline" id="Section_2.1">Section 2.1</span></h3>
293 <h2><span class="mw-headline" id="Section_3">Section 3</span></h2>
298 'Statelessly disable TOC' => [
299 [ 'allowTOC' => false ], [], $text, <<<EOF
303 <h2><span class="mw-headline" id="Section_1">Section 1</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&action=edit&section=1" title="Edit section: Section 1">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
306 <h2><span class="mw-headline" id="Section_2">Section 2</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&action=edit&section=2" title="Edit section: Section 2">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
309 <h3><span class="mw-headline" id="Section_2.1">Section 2.1</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&action=edit&section=3" title="Edit section: Section 2.1">edit</a><span class="mw-editsection-bracket">]</span></span></h3>
312 <h2><span class="mw-headline" id="Section_3">Section 3</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&action=edit&section=4" title="Edit section: Section 3">edit</a><span class="mw-editsection-bracket">]</span></span></h2>